<?php
class Admin_AdminController extends SedController 
{
	var $menus = array( 
						'index' =>'_LB_CONFIG',
						'customer' => '_LB_GARAGEHOLDER',
						'visitor' => '_LB_VISITOR',
						'garage' => '_LB_GARAGE',
						'category' => '_LB_CATEGORY',
						'package' => '_LB_PACKAGE',
						'advertise' => '_LB_ADVERTISE',
						'changepass' => '_LB_CHANGE_PASS',
						'logout' => '_LB_LOGOUT');
						
	function Admin_AdminController(){
        parent::initialize();
        parent::checkPermission();
        
		$smarty = FRM::load('smarty');
		$smarty->assign('menus', $this->menus);
	}	
    			
	function indexAction(){
		$smarty = FRM::load('smarty');
		
		$param = new Parameter();
		$arr_param = $param->getListParameter();
		FRM_XOAD::register('server', $this);
		$smarty->assign('params', $arr_param);
		$smarty->display('admin/index.html');
	}
	/**
	 * Change pass
	 */
	function changepassAction(){
		$smarty = FRM::load('smarty');
		
		if (FRM_Request::get('act')){
			$newpass = $this->getString('newpass');
			$confirm = $this->getString('confirm');
			
			if ($confirm == '' || $newpass == '' || $confirm != $newpass){
				$smarty->assign('mess', 'Updated fail!'); 	
			}
			else {
				$user = unserialize(FRM_Session::get('user_serialize'));
				$user->password = sha1($newpass);
				$user->update();
				
				$smarty->assign('mess', 'Updated sussess!'); 
			}
					
		}
		$smarty->display('admin/changepass.html');		
	}
	
	function logoutAction()
	{
		// check whether or not the current login user was Admin and it was logged by Login OneClick
		$cur_login_user = unserialize(FRM_Session::get('user_serialize'));
		$user = new User();
		if ($cur_login_user->is_controller == 1 
			&& FRM_Session::exist('retransform_id') && $user->get(FRM_Session::get('retransform_id')))
		{
			FRM_Session::remove();

			// this will be used to clarify that next is retransforming login action
			FRM_Request::set('is_retransform', 1);
			FRM_Request::set('id', $user->id);
			self::loginAction();
		}else{
			FRM_Session::remove();
		}
		
		FRM::redirect(APPLICATION_URL . '/index');
	}
	
	public function saveParam($id_param, $value)
	{
		$param = new Parameter();
		return $param->saveValue($id_param, $value);
	}
	
	public function categoryAction(){
		$smarty = FRM::load('smarty');
		
		$category = new Category();
		$action = FRM_Request::getString('action');
		
		if($action == 'add' || $action == 'edit'){
			$category->get(FRM_Request::getIntNumber('id'));
			
			if(FRM_Request::get('act')){
				
				$category->name = FRM_Request::getString('name');
				$category->description = FRM_Request::getString('description');
				
				if($category->id){
					$category->update();
				}else{
					$category->insert();
				}
				FRM::redirect(APPLICATION_URL.'/admin/category');
			}
			$smarty->assign('category', $category);
			$smarty->display('admin/category-form.html');
		}elseif ($action == 'delete'){
			$category->get(FRM_Request::getIntNumber('id'));
			$category->delete();
			$smarty->assign('mess', 'Delete successfully');
			
			FRM::redirect(APPLICATION_URL.'/admin/category');
		}else{
			$category_name = FRM_Request::getString('name');
			$filter = array('category_name' => $category_name);
			$model = $category->getList($filter);
			$url_base = APPLICATION_URL.'/admin/category';
			
			if($category_name){
				$url_base .= '/name/'.utf8_encode($category_name);
			}
			
			$smarty->assign('category_name', $category_name);
			$smarty->assign('category', $model);
			$smarty->assign('url_base', $url_base);
			$smarty->display('admin/category.html');
		}
	}
	
	public function packageAction(){
		$smarty = FRM::load('smarty');
	
		$package = new Package();
		$action = FRM_Request::getString('action');
	
		if($action == 'add' || $action == 'edit'){
			$package->get(FRM_Request::getIntNumber('id'));
				
			if(FRM_Request::get('act')){
	
				$package->name = FRM_Request::getString('name');
				$package->ad_day = FRM_Request::getString('ad_day');
				$package->search_day = FRM_Request::getString('search_day');
	
				if($package->id){
					$package->update();
					$smarty->assign('mess', 'Update successfully');
				}else{
					$package->insert();
					$smarty->assign('mess', 'Insert successfully');
				}
				
				FRM::redirect(APPLICATION_URL.'/admin/package');
			}
			$smarty->assign('package', $package);
			$smarty->display('admin/package-form.html');
		}elseif ($action == 'delete'){
			$package->get(FRM_Request::getIntNumber('id'));
			$package->delete();
				
			FRM::redirect(APPLICATION_URL.'/admin/package');
		}else{
			$model = $package->getList();
				
			$smarty->assign('package', $model);
			$smarty->display('admin/package.html');
		}
	}
	
	public function placeAction(){
		$smarty = FRM::load('smarty');
	
		$place = new Place();
		$action = FRM_Request::getString('action');
	
		if($action == 'add' || $action == 'edit'){
			$place->get(FRM_Request::getIntNumber('id'));
	
			if(FRM_Request::get('act')){
	
				$place->name = FRM_Request::getString('name');
	
				if($place->id){
					$place->update();
					$smarty->assign('mess', 'Update successfully');
				}else{
					$place->insert();
					$smarty->assign('mess', 'Insert successfully');
				}
	
				FRM::redirect(APPLICATION_URL.'/admin/place');
			}
			$smarty->assign('place', $place);
			$smarty->display('admin/place-form.html');
		}elseif ($action == 'delete'){
			$place->get(FRM_Request::getIntNumber('id'));
			$place->delete();
	
			FRM::redirect(APPLICATION_URL.'/admin/place');
		}else{
			$model = $place->getList();
	
			$smarty->assign('place', $model);
			$smarty->display('admin/place.html');
		}
	}
	
	public function advertiseAction(){
		$smarty = FRM::load('smarty');
	
		$advertise = new Advertisement();
		$action = FRM_Request::getString('action');
	
		if($action == 'add' || $action == 'edit'){
			$advertise->get(FRM_Request::getIntNumber('id'));
	
			if(FRM_Request::get('act')){
	
				$advertise->name = FRM_Request::getString('name');
				$advertise->price = FRM_Request::getIntNumber('price');
				$advertise->description = FRM_Request::getString('desc');
	
				if($advertise->id){
					$advertise->update();
					$smarty->assign('mess', 'Update successfully');
				}else{
					$advertise->insert();
					$smarty->assign('mess', 'Insert successfully');
				}
	
				FRM::redirect(APPLICATION_URL.'/admin/advertise');
			}
			$smarty->assign('advertise', $advertise);
			$smarty->display('admin/advertise-form.html');
		}elseif ($action == 'delete'){
			$advertise->get(FRM_Request::getIntNumber('id'));
			$advertise->delete();
	
			FRM::redirect(APPLICATION_URL.'/admin/advertise');
		}else{
			$model = $advertise->getList();
	
			$smarty->assign('advertise', $model);
			$smarty->display('admin/advertise.html');
		}
	}
	
	public function customerAction(){
		$smarty = FRM::load('smarty');

		$modelCustomer = new Customer();
		$action = FRM_Request::getString('action');
		
		if($action == 'view'){
			$modelCustomer->get(FRM_Request::getIntNumber('id'));
			
			$smarty->assign('customer', $modelCustomer);
			$smarty->display('admin/customer-detail.html');
		}elseif ($action == 'delete'){
			$modelCustomer->get(FRM_Request::getIntNumber('id'));
			$modelCustomer->delete();
		
			FRM::redirect(APPLICATION_URL.'/admin/customer');
		}else{
			$url_base = APPLICATION_URL.'/admin/customer';
			$date_create = FRM_Request::getString('date_create');
			
			if (Utils::isValidDate($date_create, "%m/%d/%Y") )
			{
				$date_create = date('Y-m-d', strtotime($date_create));
			}
				
			$filter = array('date_create' => $date_create);
			$customer = $modelCustomer->getList($filter);
			
			if($date_create){
				$url_base .= '/date_create/'.utf8_encode($date_create);
			}
			
			$smarty->assign('url_base', $url_base);
			$smarty->assign('customer', $customer);
			$smarty->display('admin/customer.html');
		}
	}
	
	public function garageAction(){
		$smarty = FRM::load('smarty');
	
		$modelGarage = new Garage();
		$action = FRM_Request::getString('action');
	
		if($action == 'view'){
			$garage = $modelGarage->getDetailGarage(FRM_Request::getIntNumber('id'));
			$bct = new BCT('view_garage', 'garage');
			
			$smarty->assign('garage', $garage);
			$smarty->assign('bct', $bct);
			$smarty->display('admin/garage-detail.html');
		}elseif ($action == 'delete'){
			$modelGarage->get(FRM_Request::getIntNumber('id'));
			$modelGarage->delete();
	
			FRM::redirect(APPLICATION_URL.'/admin/garage');
		}else{
			$url_base = APPLICATION_URL.'/admin/garage';
			$category_id = FRM_Request::getIntNumber('category');
			$package_id = FRM_Request::getIntNumber('package');
			$filter = array();
			$filter['category_id'] = $category_id;
			$filter['package_id'] = $package_id;
			$garage = $modelGarage->getList($filter, true);
				
			if($category_id){
				$url_base .= '/category/'.utf8_encode($category_id);
			}
			if($package_id){
				$url_base .= '/package/'.utf8_encode($package_id);
			}
			
			$modelCat = new Category();
			$category = $modelCat->getList($filter, false);
			$modelPackage = new Package();
			$package = $modelPackage->getList(false);

			$smarty->assign('category_id', $category_id);
			$smarty->assign('package_id', $package_id);
			$smarty->assign('url_base', $url_base);
			$smarty->assign('category', $category);
			$smarty->assign('package', $package);
			$smarty->assign('garage', $garage);
			$smarty->display('admin/garage.html');
		}
	}
	
	public function visitorAction(){
		$smarty = FRM::load('smarty');
	
		$modelVisitor = new Visitor();
		$action = FRM_Request::getString('action');
	
		if($action == 'active'){
			$modelVisitor->get(FRM_Request::getIntNumber('id'));
			
			if($modelVisitor->sendMail($modelVisitor)){
				$modelVisitor->status = 1;
				$modelVisitor->update();
			}
							
			FRM::redirect(APPLICATION_URL.'/admin/visitor');
		}elseif ($action == 'delete'){
			$modelVisitor->get(FRM_Request::getIntNumber('id'));
			$modelVisitor->delete();
	
			FRM::redirect(APPLICATION_URL.'/admin/visitor');
		}else{
			$url_base = APPLICATION_URL.'/admin/garage';
			$date_create = FRM_Request::getIntNumber('date_create');
			$filter = array();
			$filter['date_create'] = $date_create;
			$visitor = $modelVisitor->getList($filter, true);
	
			if($date_create){
				$url_base .= '/date_create/'.utf8_encode($date_create);
			}

			$smarty->assign('url_base', $url_base);
			$smarty->assign('visitor', $visitor);
			
			$smarty->display('admin/visitor.html');
		}
	}

}
?>